package com.logic.landseaserver.ws;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.domain.CleanLog;
import com.logic.landseaserver.service.IMeter;
import com.logic.landseaserver.ws.dto.CleanLogDTO;
import com.logic.landseaserver.ws.request.AchieveDayFreezeEnergyReq;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Author: quintina
 * @Date: 2017/7/1
 * @Description: 账单控制类
 */
@Path("/meter")
@Api(value = "鼎信水电")
public class MeterRest {

    @Autowired
    private IMeter meterService;

    private static Logger logger = LoggerFactory.getLogger(MeterRest.class);

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获得房间日冻结数据", position = 4)
    public Response retrieveProjectById(@ApiParam(value = "param", required = true) AchieveDayFreezeEnergyReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            Object object = meterService.achieveDayFreezeEnergy(req);
            map.setData(object);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("retrieveProjectById error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    @POST
    @Path("/insertAchieveDayFreezeEnergy")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "日冻结批量获取")
    public Response batchInsertAchieveDayFreezeEnergy(
            @ApiParam(value = "param", required = true) AchieveDayFreezeEnergyReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            meterService.batchInsertFeeDetail(req);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("batchInsertAchieveDayFreezeEnergy error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    @POST
    @Path("/batchUpdateFeeDetail")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "批量修改三天之内的异常水电", position = 10)
    public Response batchUpdateFeeDetail() {
        CommonResultMap map = new CommonResultMap();
        try {
            meterService.batchUpdateMeterData();
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("batchUpdateFeeDetail error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/cleanData")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "清理数据", position = 14)
    public Response cleanData( ) {
        CommonResultMap map = new CommonResultMap();
        try {
            meterService.cleanData();
            CleanLog log = new CleanLog();
            meterService.saveCleanData(log);
            map.setResult(CommonResult.SUCCESS,"cleanData success");
        } catch (Exception e) {
            logger.error("cleanData error", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    //查出日志
    @POST
    @Path("/queryCleanData")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询清除数据", position = 14)
    public Response queryCleanData() {
        CommonResultMap map = new CommonResultMap();
        try {
           List<CleanLogDTO> cleanLogDTOs =  meterService.selectCleanData();
           map.setData(cleanLogDTOs);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("clearBillPaymentDue error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
